#include "loop_queue.h"

int create_queue(queue_t **p){
    if(NULL == p){
        return -1;
    }
    *p=(queue_t *)malloc(sizeof(queue_t));
    if(NULL == *p){
        return -1;
    }
    (*p)->front = 0;
    (*p)->rear = 0;
    return 0;
}

int clean_queue(queue_t *my_queue){
    if(NULL == my_queue){
        return -1;
    }
    my_queue->front = 0;
    my_queue->rear = 0;
    return 0;
}

int destory_queue(queue_t **my_queue){
    if(NULL == my_queue || NULL == *my_queue){
        printf("入参为NULL 请检查\n");
        return -1;
    }
    free(*my_queue);
    *my_queue = NULL;
    return 0;
}

int is_full(queue_t *my_queue){
    if(NULL == my_queue){
        return -1;
    }
    return my_queue->front == ((my_queue->rear+1)%N) ? 1 : 0;
}

int push_queue(queue_t *my_queue,int num){
    if(NULL == my_queue){
        return -1;
    }
    //判断队列是否满了
    if(is_full(my_queue)){
        return -1;
    }
    my_queue->data[my_queue->rear] = num;
    my_queue->rear = (my_queue->rear+1)%N;
    return 0;
}

int is_empty(queue_t *my_queue){
    if(NULL == my_queue){
        return -1;
    }
    return my_queue->rear == my_queue->front ? 1:0;
}

int pop_queue(queue_t *my_queue,int *buff){
    if(NULL == my_queue || NULL == buff){
        return -1;
    }
    if(is_empty(my_queue)){
        return -1;
    }
    *buff = my_queue->data[my_queue->front];
    my_queue->front = (my_queue->front+1)%N;
    return 0;
}

int print_queue(queue_t *my_queue){
      if (NULL == my_queue) {
        return -1; 
    }
    if (is_empty(my_queue)) {
        printf("队列为空\n");
        return -1;
    }
    
    printf("队列元素为: ");
    /*int i = myqueue->front;
    // 循环遍历队列直到队尾
    while (i != myqueue->rear) {
        printf("%d ", myqueue->data[i]);
        i = (i + 1) % N;
    }
    */
    int i;
    for (i = my_queue->front; i != my_queue->rear; i = (i + 1) % N) {
        printf("%d ", my_queue->data[i]);
    }
    printf("\n");
    return 0; 
}
